<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib uri="http://java.sun.com/jstl/core_rt" prefix="c" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <script src="/scripts/jquery.js"></script>
    <link href="/styles/common.css" rel="stylesheet" type="text/css"/>
    <style>
    	.out{
    		padding:20px 0 0 300px ;
    	}
    	table{
    		width:400px;
    	}
    	td{
    		border: 1px solid #CCCCCC;
    	}
    	.word{
    		height:30px;
    		display:block;
    	}
    	.role_he{
    		height:10px;
    	}
    </style>
  </head>
  
  <body>
  	<input type="hidden" value="<c:out value="${updateUserRole}" />" id="isUpdate"/>
    	<div class="out">
    		<font class="word">请核对用户基本信息，在继续角色分配</font>
    		<table>
    			<tr><td>ID</td><td><c:out value="${user.id}" /></td></tr>
    			<tr><td>用户名</td><td><c:out value="${user.userName}" /></td></tr>
    			<tr><td>编号</td><td><c:out value="${user.systemNumber}" /></td></tr>
    			<tr><td>密码</td><td>********</td></tr>
    			<tr><td>工作地址</td><td>
    				<c:out value="${province.proname}" />
    				<c:if test="${province.proid != -1}">
    					<c:out value="${city.cityname}" />
    					<c:if test="${city.cityid != -1}">
    						<c:out value="${county.countyname}" />
    					</c:if>
    					<c:if test="${city.cityid == -1}">
    						<c:out value="--" />
    					</c:if>
    					
    				</c:if>
    				<c:if test="${province.proid == -1}">
    					<c:out value="--" />
    					<c:out value="--" />
    				</c:if>
    				
    			</td></tr>
    		</table>	
    	</div>
    	<div class="out">
    		<font class="word">角色列表</font>
    		<form action="/userMan/addUserRole.action" method="POST">
    			<input type="hidden" value="<c:out value="${user.id}" />" name="userId" id = "userId" />
	    		<c:forEach var="role" items="${allRoles}" >
	    			<div class="role_he"></div>
					<input type="checkbox" value="<c:out value="${role.id}"  />" name="roleid" /><c:out value="${role.roleName}"/><br/>
				</c:forEach>
				<input type="submit" value="确认" id="sub"/>
    		</form>
    	</div>
  </body>
  
</html>
<script type="text/javascript">
$(document).ready(function(){
	if($("#isUpdate").val()== "true"){
		var userId = $("#userId").val();
		$("#sub").remove();
		$("input[type='checkbox']").each(function(){
			ajaxload(userId,$(this).val(),$(this));
		})
		$("input[type='checkbox']").change(function(){
			if($(this).attr("checked")){
				ajaxCreateRole_user(userId,$(this));
			}else{
				ajaxDelRole_user(userId,$(this));
			}
		});
	}
})
function ajaxCreateRole_user(userId , dom){
	var roleId = $(dom).val();
	$.ajax({
		type:"POST",
		url:"/ajaxUser/createRoleUser.action",
		data:{"userId":userId,"roleId":roleId},
		dataType:"json",
		success:function(msg){
			var data = eval("("+msg+")");
			if(data.status){
			}else{
				alert("操作失败");
				$(dom).attr("checked",false);
			}
		},
	});
}
function ajaxDelRole_user(userId,dom){
	var roleId = $(dom).val();
	$.ajax({
		type:"POST",
		url:"/ajaxUser/deteleRoleUser.action",
		data:{"userId":userId,"roleId":roleId},
		dataType:"json",
		success:function(msg){
			var data = eval("("+msg+")");
			if(data.status){
			}else{
				alert("操作失败");
				$(dom).attr("checked",true);
			}
		},
	});
}
function ajaxload(userId , roleId ,dom){
	$.ajax({
		type:"POST",
		url:"/ajaxUser/getUserRole.action",
		data:{"userId":userId,"roleid":roleId},
		dataType:"JSON",
		success:function(msg){
			var data = eval("("+msg+")");
			if(data.status){
				$(dom).attr("checked",true);
			}
		},
	});
}
</script>